package com.hapzxb.thread.pool;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.util.concurrent.TimeUnit;

/**
 * @author hhh
 * @date 2021-05-09 17:37
 */
public class ConnectionDriver {

    static class ConnectionHandler implements InvocationHandler {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if (method.getName().equals("commit")) {
                TimeUnit.SECONDS.sleep(100);
            }
            return null;
        }
    }

    // 创建一共Connection的代理，在commit时 休眠100毫秒
    public static final Connection createConnection() {
        return (Connection) Proxy
                .newProxyInstance(ConnectionDriver.class.getClassLoader(), new Class[]{Connection.class},
                        new ConnectionHandler());
    }
}
